/*
 * 项目名称:platform-plus
 * 类名称:ExpenseServiceImpl.java
 * 包名称:com.platform.modules.custmoer.expense.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-10-10 15:52:59        李鹏军     初版做成
 *
 * Copyright (c) 2019-2019 klwu
 */
package com.platform.modules.custmoer.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.modules.custmoer.dao.ExpenseDao;
import com.platform.modules.custmoer.entity.ExpenseEntity;
import com.platform.modules.custmoer.service.ExpenseService;
import com.platform.modules.custmoer.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Service实现类
 *
 * @author 李鹏军
 * @date 2020-10-10 15:52:59
 */
@Service("expenseService")
public class ExpenseServiceImpl extends ServiceImpl<ExpenseDao, ExpenseEntity> implements ExpenseService {

    @Autowired
    private StaffService staffService;

    @Override
    public List<ExpenseEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.create_time");
        params.put("asc", false);
        Page<ExpenseEntity> page = new Query<ExpenseEntity>(params).getPage();
        List<ExpenseEntity> expenseEntities = baseMapper.selectExpensePage(page, params);
        expenseEntities.forEach(new Consumer<ExpenseEntity>() {
            @Override
            public void accept(ExpenseEntity expenseEntity) {
                if(expenseEntity.getStaffId() != null){
                    expenseEntity.setStaffName(staffService.getById(expenseEntity.getStaffId()).getName());
                }
                if(expenseEntity.getStatus() == 0){
                    expenseEntity.setStatusStr("不通过");
                }else if(expenseEntity.getStatus() == 1){
                    expenseEntity.setStatusStr("待审核");
                }else if (expenseEntity.getStatus() == 2){
                    expenseEntity.setStatusStr("已完成");
                }else if(expenseEntity.getStatus() == 3){
                    expenseEntity.setStatusStr("撤销");
                }
            }
        });
        return page.setRecords(expenseEntities);
    }

    @Override
    public boolean add(ExpenseEntity expense) {
        return this.save(expense);
    }

    @Override
    public boolean update(ExpenseEntity expense) {
        return this.updateById(expense);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
}
